package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.DishReviewDTO;
import com.sky.result.Result;
import com.sky.service.DishReviewService;
import com.sky.vo.DishReviewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品评价控制器
 */
@RestController
@RequestMapping("/user/dishReview")
@Slf4j
public class DishReviewController {

    @Autowired
    private DishReviewService dishReviewService;

    /**
     * 添加菜品评价
     * @param dishReviewDTO 菜品评价DTO
     * @return 结果
     */
    @PostMapping
    public Result<String> addReview(@RequestBody DishReviewDTO dishReviewDTO) {
        Long userId = BaseContext.getCurrentId();
        log.info("添加菜品评价：{}", dishReviewDTO.getDishId());
        log.info("添加菜品评价：{}, 用户ID：{}", dishReviewDTO, userId);
        return dishReviewService.addReview(dishReviewDTO, userId);
    }

    /**
     * 根据订单ID查询用户已评价的菜品列表
     * @param orderId 订单ID
     * @return 评价列表
     */
    @GetMapping("/order/{orderId}")
    public Result<List<DishReviewVO>> getReviewsByOrderId(@PathVariable Long orderId) {
        Long userId = BaseContext.getCurrentId();
        log.info("查询订单菜品评价：订单ID={}, 用户ID={}", orderId, userId);
        return dishReviewService.getReviewsByOrderId(orderId, userId);
    }

    /**
     * 根据菜品ID查询评价列表
     * @param dishId 菜品ID
     * @return 评价列表
     */
    @GetMapping("/dish/{dishId}")
    public Result<List<DishReviewVO>> getReviewsByDishId(@PathVariable Long dishId) {
        log.info("查询菜品评价：菜品ID={}", dishId);
        return dishReviewService.getReviewsByDishId(dishId);
    }

    /**
     * 查询订单中未评价的菜品列表
     * @param orderId 订单ID
     * @return 未评价的菜品列表
     */
    @GetMapping("/pending/{orderId}")
    public Result<List<DishReviewVO>> getUnreviewedDishByOrderId(@PathVariable Long orderId) {
        Long userId = BaseContext.getCurrentId();
        log.info("查询订单未评价菜品：订单ID={}, 用户ID={}", orderId, userId);
        return dishReviewService.getUnreviewedDishByOrderId(orderId, userId);
    }
}